package com.lanou.managerLogin.manager.web.servlet;

import com.lanou.managerLogin.manager.domain.User;
import com.lanou.managerLogin.manager.service.ManagerLoginService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/managerLoginServlet")
public class ManagerLoginServlet extends HttpServlet {

    private ManagerLoginService mls = new ManagerLoginService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String adminname = req.getParameter("adminname");
        String password = req.getParameter("password");
        User user = new User();
        user.setUsername(adminname);
        user.setPassword(password);
        boolean login = mls.login(user);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter pw = resp.getWriter();
        if (login == true) {
            req.getSession().setAttribute("adminname", adminname);
            System.out.println("登录成功");
            req.getRequestDispatcher("/adminjsps/admin/main.jsp").forward(req, resp);
        } else {
            pw.write("登录失败，3秒后返回登录页面");
            resp.setStatus(302);
            resp.setHeader("refresh", "3;/adminjsps/login.jsp");
            System.out.println("登录失败");
        }
    }
}
